<?php
namespace tp5_lib\account\logic;

use tp5_lib\common\logic\Logic;
use tp5_lib\account\extension\PaymentTrade;
use tp5_lib\account\model\RechargeModel;
use tp5_lib\user\model\UserModel;
use tp5_lib\account\model\AccountLogModel;
use think\Db;

class RechargeLogic extends Logic implements PaymentTrade
{
    /**
     * 创建充值订单
     */
    public function create($uid,$amount)
    {
        $data=[
            "user_id"=>$uid,
            "amount"=>$amount,
            "time"=>time(),
        ];
        $model=new RechargeModel();
        if(!$id=$model->db()->insertGetId($data)){
            $this->error("创建失败");
        }
        return $id;
    }
    public function paySuccess($id)
    {
        $user_model=new UserModel();
        $log_model=new AccountLogModel();
        $model=new RechargeModel();
        /** @var Query $db */
        $db=Db::connect();
        $db->startTrans();
        try {
            if(!$record=$model->db()->where(["id"=>$id])->find()){
                $this->error="订单不存在";
                return false;
            }
            if($record["status"] != "0"){
                return true;
            }
            $sql=$user_model->db()->where(["id"=>$record["user_id"]])->fetchSql()->find();
            if(!$set=$db->query($sql)){
                $db->rollback();
                $this->error="用户不存在";
                return false;
            }
            $user=$set[0];
            
            $sql=$log_model->db()->fetchSql()->insert([
                "handler"=>$record["user_id"],
                "user_id"=>$record["user_id"],
                "previous"=>$user["money"],
                "money"=>$record["amount"],
                "type"=>"recharge",
                "asso_id"=>$id,
                "message"=>"用户充值",
                "time"=>time(),
            ]);
            if(!$db->execute($sql)){
                $db->rollback();
                $this->error="插入记录失败";
                return false;
            }

            $sql=$user_model->where(["id"=>$record["user_id"]])->fetchSql()->setInc("money",$record["amount"]);
            if(!$db->execute($sql)){
                $db->rollback();
                $this->error="修改余额失败";
                return false;
            }
            $sql=$model->db()->where(["id"=>$id])->fetchSql()->setField("status",1);
            $db->execute($sql);
            $db->commit();
            return true;
        }
        catch (\Exception $ex) {
            $db->rollback();
            $this->error="数据库操作错误:".$ex->getMessage();
            return false;
        }
    }
    public function payClose($id)
    {
        $model=new RechargeModel();
        if(!$record=$model->db()->where(["id"=>$id])->find()){
            $this->error="订单不存在";
            return false;
        }
        if($record["status"] == 1){
            $this->error="已支付";
            return false;
        }
        $model->db()->where(["id"=>$id])->setField("status","2");
        return true;
    }
    public function payStatus($id)
    {
        $model=new RechargeModel();
        if(!$record=$model->db()->where(["id"=>$id])->find()){
            $this->error="订单不存在";
            return false;
        }
        if($record["status"] != 1){
            $this->error="未支付";
            return false;
        }
        return true;
    }
}
